package cn.hdu.liang.ch5;

import cn.hdu.liang.ch4.Dish;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static cn.hdu.liang.ch4.Dish.menu;

public class Finding {
    public static void main(String[] args) {
        if(menu.stream().anyMatch(Dish::isVegetarian)) {
            System.out.println("这里面有蔬菜");
        }

        boolean isHealthy = menu.stream()
                .allMatch(d -> d.getCalories() < 1000);
        System.out.println(isHealthy);

        isHealthy = menu.stream()
                .noneMatch(d -> d.getCalories() >= 1000);
        System.out.println(isHealthy);

        Optional<Dish> dish = menu.stream()
                .filter(Dish::isVegetarian)
                .findAny();
        System.out.println(dish);

        menu.stream()
                .filter(Dish::isVegetarian)
                .findAny()
                .ifPresent(d -> System.out.println(d.getName()));

        List<Integer> someNum = Arrays.asList(1, 2, 3, 4, 5);
        Optional<Integer> numFirst = someNum.stream()
                .map(x -> x * x)
                .filter(x -> x % 3 == 0)
                .findFirst();
        System.out.println(numFirst.get());

    }
}
